#include "sbr.h"
#include "rectype.h"
#include "util.h"

#include "nmfield.h"
#include "nmfield.cpp"
#include "cnfield.h"

using namespace std;
using namespace opsrc::stdf;

Sbr::Sbr()
{
}

Sbr::Sbr(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex,  endian e, char* buf)
:RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex),en(e) {
    if (buf!=0)
        readFields(buf);
}

Sbr::~Sbr(){
}


const StdfField<int> Sbr::getHeadNum() const {
    return head_num;
}

void Sbr::setHeadNum(StdfField<int> headnum) {
    head_num = headnum;
}

const StdfField<int> Sbr::getSiteNum() const {
    return site_num;
}

void Sbr::setSiteNum(StdfField<int> sitenum) {
    site_num = sitenum;
}

const StdfField<int> Sbr::getSbinNum() const {
    return sbin_num;
}

void Sbr::setSbinNum(StdfField<int> sbinnum) {
    sbin_num = sbinnum;
}

const StdfField<long> Sbr::getSbinCount() const {
    return sbin_cnt;
}

void Sbr::setSbinCount(StdfField<long> sbincnt) {
    sbin_cnt = sbincnt;
}

const StdfField<string> Sbr::getSbinPassFail() const {
    return sbin_pf;
}

void Sbr::setSbinPassFail(StdfField<string> sbinpf) {
    sbin_pf = sbinpf;
}

const StdfField<string> Sbr::getSbinName() const {
    return sbin_nam;
}

void Sbr::setSbinName(StdfField<string> sbinnam) {
    sbin_nam = sbinnam;
}

void Sbr::readFields(char *buf) {

#if _DEBUG
    cout << "stdfrec= SBR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", recbytecount= " << reclen <<endl;
#endif

    int curpos = 0;
    // Read head number , 1 byte
    NmField<unsigned int, 1> headnum(buf+curpos, en);
    head_num.set(headnum.getData(), curpos, headnum.getByteCount(), headnum.getTotalByteCount());
    curpos += head_num.getTotalByteCount();

    // Read site num, 1 byte
    if (curpos < reclen -1) {
        NmField<unsigned int, 1> sitenum(buf+curpos, en);
        site_num.set(sitenum.getData(), curpos, sitenum.getByteCount(), sitenum.getTotalByteCount());
        curpos += site_num.getTotalByteCount();
    }

    // Read sbin num 2 byte
    if (curpos < reclen -1) {
        NmField<unsigned int, 2> sbinnum(buf+curpos, en);
        sbin_num.set(sbinnum.getData(), curpos, sbinnum.getByteCount(), sbinnum.getTotalByteCount());
        curpos += sbin_num.getTotalByteCount();
    }

    // Read sbin count
    if (curpos < reclen -1) {
        NmField<unsigned int, 4> sbincnt(buf+curpos, en);
        sbin_cnt.set(sbincnt.getData(), curpos, sbincnt.getByteCount(), sbincnt.getTotalByteCount());
        curpos += sbin_cnt.getTotalByteCount();
    }
    
    // read sbin pass fail
    if (curpos < reclen-1) {
        CnField<string,1> sbinpf(buf+curpos, en);
        sbin_pf.set(sbinpf.getData(), curpos, sbinpf.getByteCount(), sbinpf.getTotalByteCount());
        curpos += sbin_pf.getTotalByteCount();
    }

    // read sbin name
    if (curpos < reclen-1) {
        CnField<string> sbinnam(buf+curpos, en);
        sbin_nam.set(sbinnam.getData(), curpos, sbinnam.getByteCount(), sbinnam.getTotalByteCount());
    }
}

